package com.qsl.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsl.common.result.Result;
import com.qsl.common.util.MD5;
import com.qsl.model.system.SysUser;
import com.qsl.model.vo.SysUserQueryVo;
import com.qsl.system.annotation.LogAnnotation;
import com.qsl.system.enums.BusinessType;
import com.qsl.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author qsl
 * @since 2023-03-26
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/SysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit, SysUserQueryVo sysUserQueryVo) {

        Page<SysUser> pageParam = new Page<>(page, limit);
        IPage<SysUser> pageMode = sysUserService.selectPage(pageParam, sysUserQueryVo);

        return Result.ok(pageMode);
    }

    @ApiOperation("修改用户状态")
    @GetMapping("/updateStatus/{Id}/{status}")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.UPDATE) // 自定义注解-操作日志—新增
    public Result updateStatus(@PathVariable String Id, @PathVariable Integer status) {
        SysUser sysUser = new SysUser();
        sysUser.setId(Id);
        sysUser.setStatus(status);
        boolean flag = sysUserService.updateById(sysUser);

        return Result.ok(flag);
    }

    @ApiOperation("新增用户")
    @PutMapping("save")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.INSERT) // 自定义注解-操作日志—新增
    public Result saveUser(@RequestBody SysUser sysUser) {
        // MD5密码加密
        String encrypt = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(encrypt); // 密码加密后

        boolean flag = sysUserService.save(sysUser);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }


    @ApiOperation("修改用户-根据id查询用户")
    @GetMapping("getUser/{Id}")
    public Result getUser(@PathVariable Long Id) {
        SysUser User = sysUserService.getById(Id);
        return Result.ok(User);
    }

    @ApiOperation("修改用户")
    @PostMapping("updateUser")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.UPDATE) // 自定义注解-操作日志—新增
    public Result updateUser(@RequestBody SysUser sysUser) {
        boolean flag = sysUserService.updateById(sysUser);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }

    @ApiOperation("删除用户")
    @DeleteMapping("removeUser/{Id}")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.DELETE) // 自定义注解-操作日志—新增
    public Result removeUser(@PathVariable Long Id) {
        boolean flag = sysUserService.removeById(Id);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }

    @ApiOperation("批量删除")
    @DeleteMapping("removeUser")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.DELETE) // 自定义注解-操作日志—新增
    public Result bachRemove(@RequestBody List<Long> Ids) {
        boolean flag = sysUserService.removeByIds(Ids);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }


}

